//
//  NewsMadeViewController.m
//  homeland
//
//  Created by ios on 14-5-29.
//  Copyright (c) 2014年 xzysun. All rights reserved.
//

#import "NewsMadeViewController.h"
#import "MyConcernConfigListViewController.h"
#import "MyConcernConfigService.h"
#import "MyConcernNoticesDetailViewController.h"
#import "MyConcernDataService.h"

@interface NewsMadeViewController ()
@property (strong, nonatomic) MyConcernDataService *dataService;
@property (assign, nonatomic) BOOL onSaving;

@end

@implementation NewsMadeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"信息定制";
    
    self.dataService = [[MyConcernDataService alloc]init];
    _dataService.delegate = self;
    self.onSaving = NO;

    
    
    [self.tableView setBackgroundView:nil];
    [self.tableView setBackgroundColor:[UIColor clearColor]];
}


#pragma mark - Table Method
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;//信息定制有4个section

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44.f;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
        if (indexPath.section == 0) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyConcernConfigCell"];
            cell.textLabel.text = @"要情动态";
            if ([MyConcernConfigService getService].selectSceneCategoryType == 0) {
                cell.accessoryType = UITableViewCellAccessoryNone;
            } else {
                cell.accessoryType = UITableViewCellAccessoryCheckmark;
            }
        } else if (indexPath.section == 1) {
            //通知公告
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyConcernConfigCell"];
            cell.textLabel.text = @"通知公告";
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }else if (indexPath.section == 2) {
            //政策文件
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyConcernConfigCell"];
            cell.textLabel.text = @"政策文件";
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }else if (indexPath.section == 3) {
            //提交按钮
            cell = [[[NSBundle mainBundle] loadNibNamed:@"MyConcernConfigButtonCell" owner:self options:nil] objectAtIndex:0];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section == 0) {
        //定制要情动态
            if ([MyConcernConfigService getService].selectSceneCategoryType == 0) {
                [MyConcernConfigService getService].selectSceneCategoryType = 1;
            } else {
                [MyConcernConfigService getService].selectSceneCategoryType = 0;
            }
            [self.tableView reloadData];
    }
    else if (indexPath.section == 1) {
        //定制通知公告
        MyConcernConfigListViewController *listVC = [[MyConcernConfigListViewController alloc]initWithNibName:@"MyConcernConfigListViewController" bundle:nil];
        [MyConcernConfigService getService].currentConfigType = MyConcernConfigTypeNotice;
        [self.navigationController pushViewController:listVC animated:YES];
    } else if (indexPath.section == 2) {
        //定制政策文件
        MyConcernConfigListViewController *listVC = [[MyConcernConfigListViewController alloc]initWithNibName:@"MyConcernConfigListViewController" bundle:nil];
        [MyConcernConfigService getService].currentConfigType = MyConcernConfigTypeFile;
        [self.navigationController pushViewController:listVC animated:YES];
    } else if (indexPath.section == 3) {
        //保存按钮
            [self saveDatas];
    }
}

-(void) saveDatas
{
    self.onSaving = YES;
    [self startLoadingViewWithText:@"正在保存..."];
    [_dataService saveMyConcernConfigs];
}


#pragma mark - Data Delegate
-(void)loadDataFinished
{
    [self stopLoadingView];
    [_tableView reloadData];
    if (self.onSaving) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"操作成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
}

-(void)loadDataFaild:(NSString *)msg
{
    [self stopLoadingView];
    [_tableView reloadData];
    
    if (self.onSaving) {
        msg = @"保存定制信息失败";
    }
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"错误" message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
